package com.bookmall.action;

import com.bookmall.MallConst;
import com.bookmall.dao.AccountDAO;
import com.bookmall.model.Account;
import com.bookmall.model.LoginAccount;
import com.bookmall.model.ModifyPasswordRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created by Squking on 2018/5/6.
 */
@Controller
public class AccountController {
    @RequestMapping(value = "/regist", method = RequestMethod.GET)
    public ModelAndView regist() {
        return new ModelAndView("regist", "command", new Account());
    }

    @RequestMapping(value = "/action/addAccount", method = RequestMethod.POST)
    public String addAcount(Account account,
                             ModelMap model) {
        model.addAttribute("title", "注册结果");
        AccountDAO dao = new AccountDAO();
        try {
            Account existAccount = dao.getAccount(account.getName());
            if (existAccount != null)
            {
                model.addAttribute("message", "用户名已注册");
            }
            else
            {
                dao.insert(account);
                model.addAttribute("message", "成功");
            }
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
            e.printStackTrace();
        }
        return "message";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login() {
        return new ModelAndView("login", "command", new LoginAccount());
    }

    @RequestMapping(value = "/action/login", method = RequestMethod.POST)
    public String login(LoginAccount loginAccount,
                        ModelMap model, HttpSession httpSession) {
        model.addAttribute("title", "登录结果");
        AccountDAO dao = new AccountDAO();
        try {
            Account account = dao.getAccount(loginAccount.getName(), loginAccount.getPassword());
            if (account != null)
            {
                httpSession.setAttribute(MallConst.SessionKey_LoginName, loginAccount.getName());
                model.addAttribute("message", "成功");
            }
            else
            {
                model.addAttribute("message", "用户不存在");
            }
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());
            e.printStackTrace();
        }
        return "message";
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public ModelAndView password(HttpSession httpSession) {
        boolean isLogin = httpSession.getAttribute(MallConst.SessionKey_LoginName) != null;
        if (isLogin)
        {
            return new ModelAndView("password", "command", new ModifyPasswordRequest());
        }
        else
        {
            ModelAndView modelAndView = new ModelAndView("message");
            modelAndView.addObject("message", "请先登录");
            return modelAndView;
        }
    }

    @RequestMapping(value = "/action/modifyPassword", method = RequestMethod.POST)
    public ModelAndView modifyPassword(ModifyPasswordRequest modifyPasswordRequest, HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView("message");
        boolean isLogin = httpSession.getAttribute(MallConst.SessionKey_LoginName) != null;
        if (isLogin)
        {
            String logname = httpSession.getAttribute(MallConst.SessionKey_LoginName).toString();
            String oldPassword = modifyPasswordRequest.getOldPassword();
            AccountDAO dao = new AccountDAO();
            try {
                Account account = dao.getAccount(logname, oldPassword);
                if (account != null)
                {
                    dao.updatePassword(logname, oldPassword, modifyPasswordRequest.getNewPassword());
                    modelAndView.addObject("message", "修改成功");
                }
                else
                {
                    modelAndView.addObject("message", "用户名或密码不正确");
                }
            } catch (Exception e) {
                modelAndView.addObject("message", e.getMessage());
                e.printStackTrace();
            }
        }
        else
        {
            modelAndView.addObject("message", "请先登录");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/accountInfo", method = RequestMethod.GET)
    public ModelAndView accountInfo(HttpSession httpSession) {
        boolean isLogin = httpSession.getAttribute(MallConst.SessionKey_LoginName) != null;
        if (isLogin)
        {
            return new ModelAndView("accountInfo", "command", new Account());
        }
        else
        {
            ModelAndView modelAndView = new ModelAndView("message");
            modelAndView.addObject("message", "请先登录");
            return modelAndView;
        }
    }

    @RequestMapping(value = "/action/modifyAccountInfo", method = RequestMethod.POST)
    public ModelAndView modifyAccountInfo(Account account, HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView("message");
        boolean isLogin = httpSession.getAttribute(MallConst.SessionKey_LoginName) != null;
        if (isLogin)
        {
            String logname = httpSession.getAttribute(MallConst.SessionKey_LoginName).toString();
            AccountDAO dao = new AccountDAO();
            try {
                Account existAccount = dao.getAccount(logname);
                if (existAccount != null)
                {
                    dao.updateAccount(logname, account);
                    modelAndView.addObject("message", "修改成功");
                }
                else
                {
                    modelAndView.addObject("message", "用户名或密码不正确");
                }
            } catch (Exception e) {
                modelAndView.addObject("message", e.getMessage());
                e.printStackTrace();
            }
        }
        else
        {
            modelAndView.addObject("message", "请先登录");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model, HttpSession httpSession) {
        boolean isLogin = httpSession.getAttribute(MallConst.SessionKey_LoginName) != null;
        if (isLogin)
        {
            httpSession.removeAttribute(MallConst.SessionKey_LoginName);
            model.addAttribute("message", "退出成功");
        }
        else
        {
            model.addAttribute("message", "还没登陆，不用退出");
        }
        return "message";
    }
}
